package com.stars.home.activity

import android.app.Activity
import android.content.Intent
import androidx.core.net.toUri
import com.luck.picture.lib.PictureSelector
import com.stars.home.base.MBActivity
import com.stars.home.config.MIntentAction
import com.stars.home.databinding.ActivityOwnerBinding
import com.stars.home.inte.OnUploadCallback
import com.stars.home.model.bean.UpImgBean
import com.stars.home.utils.FileUtils
import com.stars.home.utils.OssUtils
import com.stars.home.vmodel.OwnerVModel
import java.util.ArrayList

/**
 * 货主认证
 *on 2021/5/31
 */
class OwnerActivity : MBActivity<OwnerVModel, ActivityOwnerBinding>(), OnUploadCallback<UpImgBean> {


    override fun vmClass(): Class<OwnerVModel> {
        return OwnerVModel::class.java
    }
    override fun binding(): ActivityOwnerBinding {
       return ActivityOwnerBinding.inflate(layoutInflater)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) {
            return
        }
        if (requestCode == MIntentAction.REQUEST_CODE_THREE) {//图片

            val selectList = PictureSelector.obtainMultipleResult(data)
            val list = ArrayList<String>()
            if (null != selectList) {
                if (selectList[0].path.contains("content://")) {

                    val cr = this.contentResolver
                    list.add(FileUtils.getFilePathFromContentUri(selectList[0].path.toUri(), cr))

                } else {
                    list.add(selectList[0].path)
                }

                when (vm.type) {
                    1 -> vm.isOne.value = true
                    2 -> vm.isTwo.value = true

                }

                OssUtils(this, vm)
                        .setUpLoadMore("car", list, vm.type, this)


            }

            return

        }
        if (requestCode == MIntentAction.REQUEST_CODE_CAMERA) {//拍照
            //拍照
            val selectList = PictureSelector.obtainMultipleResult(data) ?: return
            val list = ArrayList<String>()
            if (selectList[0].path.contains("content://")) {

                val cr = this.contentResolver
                list.add(FileUtils.getFilePathFromContentUri(selectList[0].path.toUri(), cr))

            } else {
                list.add(selectList[0].path)
            }

            when (vm.type) {
                1 -> vm.isOne.value = true
                2 -> vm.isTwo.value = true
            }
            OssUtils(this, vm)
                    .setUpLoadMore("car", list, vm.type, this)


            return
        }
    }

    override fun onUploadSuccess(t: UpImgBean) {
        var index = t.position

        when (index) {
            1 -> {
                vm.imgIDMan.value = t.url
                vm.imgIDMan_up = t.urlPath
                //vm.discernID(ss[1])
                vm.isOne.value = false

            }
            2 -> {
                vm.imgIDEmblem.value = t.url
                vm.imgIDEmblem_up = t.urlPath
                vm.isTwo.value = false
            }

        }
    }

    override fun onUploadError(error: String?) {
        TODO("Not yet implemented")
    }
}
